
/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <stdio.h>

#include "libmooigraph.h"
#include "lmg.h"

void
lmg_nodelistadd (struct lmgnode *node)
{
  struct lmgnodel *ptr;
  ptr = lmg_malloc (sizeof (struct lmgnodel));
  ptr->node = node;
  if (lmgnodelist)
    {
      lmgnodelistend->next = ptr;
      lmgnodelistend = ptr;
    }
  else
    {
      lmgnodelist = ptr;
      lmgnodelistend = ptr;
    }
  return;
}

void
lmg_nodelistclear (void)
{
  struct lmgnodel *ptr;
  struct lmgnodel *ptr2;
  struct lmgedgel *el;
  struct lmgedgel *el2;
  ptr = lmgnodelist;
  while (ptr)
    {
      ptr2 = ptr->next;
      if (ptr->node->poe)
	{
	  el = ptr->node->poe;
	  while (el)
	    {
	      el2 = el->next;
	      lmg_free (el);
	      el = el2;
	    }
	}
      lmg_free (ptr->node);
      lmg_free (ptr);
      ptr = ptr2;
    }
  lmgnodelist = NULL;
  lmgnodelistend = NULL;
  return;
}

void
lmg_edgelistadd (struct lmgnode *from, struct lmgnode *to, int color, int style)
{
  struct lmgedgel *ptr;
  struct lmgedge *e;
  ptr = lmg_malloc (sizeof (struct lmgedgel));
  e = lmg_malloc (sizeof (struct lmgedge));
  ptr->edge = e;
  ptr->edge->from = from;
  ptr->edge->to = to;
  ptr->edge->color = color;
  ptr->edge->style = style;
  if (lmgedgelist)
    {
      lmgedgelistend->next = ptr;
      lmgedgelistend = ptr;
    }
  else
    {
      lmgedgelist = ptr;
      lmgedgelistend = ptr;
    }
  return;
}

void
lmg_edgelistclear (void)
{
  struct lmgedgel *ptr;
  struct lmgedgel *ptr2;
  ptr = lmgedgelist;
  while (ptr)
    {
      ptr2 = ptr->next;
      lmg_free (ptr->edge);
      lmg_free (ptr);
      ptr = ptr2;
    }
  lmgedgelist = NULL;
  lmgedgelistend = NULL;
  return;
}

struct lmgnodel *
lmg_dummynode (void)
{
  struct lmgnodel *nl;
  struct lmgnode *node;
  nl = lmg_malloc (sizeof (struct lmgnodel));
  node = lmg_malloc (sizeof (struct lmgnode));
  node->name = (char *) 0;
  nl->node = node;
  if (lmgnodelist)
    {
      lmgnodelistend->next = nl;
      lmgnodelistend = nl;
    }
  else
    {
      lmgnodelist = nl;
      lmgnodelistend = nl;
    }
  return (nl);
}

struct lmgedgel *
lmg_addedge2 (struct lmgnode *from, struct lmgnode *to)
{
  struct lmgedgel *el;
  struct lmgedge *edge;
  el = lmg_malloc (sizeof (struct lmgedgel));
  edge = lmg_malloc (sizeof (struct lmgedge));
  el->edge = edge;
  edge->from = from;
  edge->to = to;
  if (lmgedgelist)
    {
      lmgedgelistend->next = el;
      lmgedgelistend = el;
    }
  else
    {
      lmgedgelist = el;
      lmgedgelistend = el;
    }
  return (el);
}

void
lmg_deledge (struct lmgedgel *el)
{
  struct lmgedgel *eptr;
  if (el == lmgedgelist)
    {
      lmgedgelist = el->next;
      if (lmgedgelist == NULL)
	{
	  lmgedgelistend = NULL;
	}
      lmg_free (el->edge);
      lmg_free (el);
    }
  eptr = lmgedgelist;
  while (eptr)
    {
      if (eptr->next == el)
	{
	  break;
	}
      eptr = eptr->next;
    }
  if (eptr == NULL)
    {
      return;
    }
  eptr->next = el->next;
  lmg_free (el->edge);
  lmg_free (el);
  return;
}

/* End */
